using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using WriteAPI.Command.采购;

namespace WriteAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class CgddController : ControllerBase
    {
        private readonly IMediator _mediator;

        public CgddController(IMediator mediator)
        {
            _mediator = mediator;
        }

        [HttpPost("create")]
        public async Task<IActionResult> CreateCgdd([FromBody] CreateCgddCommand command)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                var result = await _mediator.Send(command);
                return Ok(new { Id = result, Message = "采购订单创建成功" });
            }
            catch (System.Exception ex)
            {
                return BadRequest(new { Message = ex.Message });
            }
        }

        [HttpPut("update")]
        public async Task<IActionResult> UpdateCgdd([FromBody] UpdateCgddCommand command)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                var result = await _mediator.Send(command);
                return Ok(new { Success = result, Message = "采购订单更新成功" });
            }
            catch (System.Exception ex)
            {
                return BadRequest(new { Message = ex.Message });
            }
        }

        [HttpDelete("delete/{id}")]
        public async Task<IActionResult> DeleteCgdd(int id)
        {
            try
            {
                var command = new DeleteCgddCommand { Id = id };
                var result = await _mediator.Send(command);
                return Ok(new { Success = result, Message = "采购订单删除成功" });
            }
            catch (System.Exception ex)
            {
                return BadRequest(new { Message = ex.Message });
            }
        }
    }
} 